package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Eventhazard;
import cn.edu.scau.cmi.ema.domain.Hazard;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("EventhazardController")
public class EventhazardController extends CmiEmaController {

	@RequestMapping("/listEventhazards")
	public ModelAndView listEventhazards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = eventhazardPaginationService.updatePagination(request, pagination);
		Set<Eventhazard> eventhazards = eventhazardPaginationService.getPaginationEventhazards(pagination);

		mav.addObject("eventhazards", eventhazards);
		mav.addObject("entityName", "eventhazard");
		mav.addObject("pagination", pagination);
		mav.setViewName("eventhazard/listEventhazards");
		return mav;
	}

	public String listEventhazards() {
		return "redirect:/listEventhazards";
	}

	@RequestMapping("/viewEventhazard")
	public ModelAndView viewEventhazard(@RequestParam Integer eventhazardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("eventhazard", eventhazardDAO.findEventhazardByPrimaryKey(eventhazardId));
		mav.setViewName("eventhazard/viewEventhazard");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEventhazards")
	public ModelAndView confirmDeleteEventhazards(
			@RequestParam(value = "deleteEventhazardsIds", required = true) Set<Integer> deleteEventhazardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Eventhazard> deleteEventhazards = eventhazardDAO.findEventhazardsByIds(deleteEventhazardsIds);
		Set<Eventhazard> referencedEventhazards = eventhazardService.loadReferenceEventhazards(deleteEventhazards);
		boolean hasReferenced;
		if (referencedEventhazards.size() == 0) {
			mav.addObject("deleteEntitys", deleteEventhazards);
			hasReferenced = false;
		} else {
			deleteEventhazards.removeAll(referencedEventhazards);
			mav.addObject("referencedEntitys", referencedEventhazards);
			mav.addObject("deleteEntitys", deleteEventhazards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Eventhazard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEventhazards");
		return mav;
	}

	@RequestMapping("/deleteEventhazard")
	public String deleteEventhazard(@RequestParam Integer eventhazardId) {
		Eventhazard eventhazard = eventhazardDAO.findEventhazardByPrimaryKey(eventhazardId);
		eventhazardService.deleteEventhazard(eventhazard);
		return "forward:/listEventhazards";
	}

	@RequestMapping("/confirmDeleteEventhazard")
	public ModelAndView confirmDeleteEventhazard(@RequestParam Integer eventhazardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("eventhazard", eventhazardDAO.findEventhazardByPrimaryKey(eventhazardId));

		mav.setViewName("eventhazard/deleteEventhazard");

		return mav;
	}

	@RequestMapping("/deleteEventhazards")
	public String deleteEventhazards(
			@RequestParam(value = "deleteEventhazardsId", required = true) Set<Integer> deleteEventhazardsId) {
		eventhazardService.deleteEventhazards(deleteEventhazardsId);
		return "forward:/listEventhazards";
	}

	@RequestMapping("/editEventhazard")
	public ModelAndView editEventhazard(@RequestParam Integer eventhazardId) {

		ModelAndView mav = new ModelAndView();

		Eventhazard eventhazard = eventhazardDAO.findEventhazardByPrimaryKey(eventhazardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("eventhazard", eventhazard);

		// 把所有的引用eventhazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventhazardService.loadEventhazardObjectReferencedSet(eventhazard));

		// 将编辑页面eventhazard添加到mav中
		mav.setViewName("eventhazard/editEventhazard");
		return mav;
	}

	@RequestMapping("/newEventhazard")
	public ModelAndView newEventhazard() {
		ModelAndView mav = new ModelAndView();
		Eventhazard eventhazard = new Eventhazard();

		mav.addObject("eventhazard", eventhazard);

		// 把所有的引用eventhazard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventhazardService.loadEventhazardObjectReferencedSet(eventhazard));

		mav.addObject("newFlag", true);
		mav.setViewName("eventhazard/editEventhazard");

		return mav;
	}

	@RequestMapping("/saveEventhazard")
	public ModelAndView saveEventhazard(@ModelAttribute Eventhazard eventhazard) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = eventhazard.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Hazard hazardTemp = eventhazard.getHazard();
		Hazard hazard = hazardTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(hazardDAO.findHazardById(hazardTemp.getId()));

		Eventhazard existingEventhazard = CmiHibernateProxyUtil
				.deproxy(eventhazardDAO.findEventhazardByPhysicalPrimaryKey(event.getId(), hazard.getId()));

		eventhazard.setEvent(event);
		eventhazard.setHazard(hazard);
		// 新建记录
		if (eventhazard.getId() == null) {
			if (eventhazard.equals(existingEventhazard)) {

				mav.addAllObjects(eventhazardService.loadEventhazardClassReferencedSet(eventhazard));
				// mav = editContinue(mav, eventhazard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventhazard/editEventhazard");
			} else {
				eventhazardService.saveEventhazard(eventhazard);
				mav.setViewName("forward:/listEventhazards");
			}
		}
		// 编辑记录
		else {
			Eventhazard originEventhazard = CmiHibernateProxyUtil
					.deproxy(eventhazardDAO.findEventhazardByPrimaryKey(eventhazard.getId()));
			if (!eventhazard.equals(originEventhazard) && eventhazard.equals(existingEventhazard)) {
				mav.addAllObjects(eventhazardService.loadEventhazardClassReferencedSet(eventhazard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventhazard/editEventhazard");
			} else {
				eventhazardService.saveEventhazard(eventhazard);
				mav.setViewName("forward:/listEventhazards");
			}
		}
		return mav;
	}

	@RequestMapping("/eventhazardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
